<?php

namespace App\Http\Controllers\V1\Api;


use App\Models\V1\Config;
use App\Models\V1\Database;
use App\Models\V1\Game;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DatabaseController extends BaseController
{

    /**
     * 游戏列表-api
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = Database::query();
        $id = auth('box')->id();
        $query->where('state', '=', 1);
        $query->where('users_id', '=', $id);
        $pageSize = $request->input('pageSize', 10);
        $sort = $request->input('sort', 'id');
        $order = $request->input('order', 'desc');
        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


    /**
     * 增加游戏-api
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

//        state 状态，1：创建，2：完善资料 3：提交代码 ，4：审核通过 5:审核失败

        $model = new Database();
        $id = auth('box')->id();
        $model->create([
            'users_id' => $id,
            'name' => $request->input('name', ""),
            'engine_version' => $request->input('engine_version', ""),
            'remark' => $request->input('remark', ''),
            'state' => $request->input('state', 1)
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 游戏详情-api
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo = Database::find($id);
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 更新游戏-api
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $vo = Database::find($id);
        $user_id = auth('box')->id();
        $vo->update([
            'users_id' => $user_id,
            'name' => $request->input('name', ""),
            'engine_version' => $request->input('engine_version', ""),
            'remark' => $request->input('remark', ''),
            'state' => $request->input('state', 1)
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 删除游戏-api
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Database::destroy($id);
        return ajax_output(200, "成功");
    }
}
